/******************************************************************************
 * Copyright 2022 The Airos Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *****************************************************************************/

#pragma once
/**
 *  双向循环链表
 **/
namespace air {
namespace link {

class Node {
 public:
  Node() : prev(nullptr), next(nullptr) {}
  virtual ~Node() {}

  Node* prev;

  Node* next;
};

class List {
 public:
  List() { Init(); }

  ~List() { Clear(false); }

  void AddHead(Node* node);

  void AddTail(Node* node);

  void Del(Node* node, bool b = false);

  void DelHead(bool b = false);

  void DelTail(bool b = false);

  void DelWithIndex(int index, bool b = false);  // not useful

  void MoveHead(Node* node);

  void MoveTail(Node* node);

  void Append(List* from);

  Node* Begin() { return m_root_.next; }

  Node* End() { return &m_root_; }

 public:
  Node* GetData(int index);  // not useful

  int Count() { return m_count_; }

  bool IsEmpty() { return &m_root_ == m_root_.next; }

  void Clear(bool b = false);

 private:
  void Init();

  void Add(Node* prev, Node* next, Node* node);

  void Del(Node* prev, Node* next, bool b);

  Node m_root_;

  int m_count_;
};

}  // namespace link
}  // namespace air
